package pvn.com.utils;

public class StringSplit {
    public static String[] split(String strString, String strDelimiter) {
        int iOccurrences = 0;
        int iIndexOfInnerString = 0;
        int iIndexOfDelimiter = 0;
        int iCounter = 0;

        if (strString == null) {
            throw new IllegalArgumentException("Input string cannot be null.");
        }

        if ((strDelimiter.length() <= 0) || (strDelimiter == null)) {
            throw new IllegalArgumentException("Delimeter cannot be null or empty.");
        }

        if (strString.startsWith(strDelimiter)) {
            strString = strString.substring(strDelimiter.length());
        }

        if (!strString.endsWith(strDelimiter)) {
            strString = strString + strDelimiter;
        }

        while ((iIndexOfDelimiter = strString.indexOf(strDelimiter, iIndexOfInnerString)) != -1) {
            iOccurrences++;
            iIndexOfInnerString = iIndexOfDelimiter + strDelimiter.length();
        }

        String[] strArray = new String[iOccurrences];

        iIndexOfInnerString = 0;
        iIndexOfDelimiter = 0;

        while ((iIndexOfDelimiter = strString.indexOf(strDelimiter, iIndexOfInnerString)) != -1) {
            strArray[iCounter] = strString.substring(iIndexOfInnerString, iIndexOfDelimiter);

            iIndexOfInnerString = iIndexOfDelimiter + strDelimiter.length();

            iCounter++;
        }

        return strArray;
    }
}